﻿using Mate.Caching;
using Mate.Db.Model.Merchant;
using Mate.Db.SqlSugar;
using Mate.Dto.Merchant;
using SqlSugar;
using System;
using System.Linq;
using System.Collections.Generic;

namespace Mate.Merchant.Service
{
    public class MerchantCategoryService : DataService<mp_merchant_category>, IMerchantCategoryService
    {
        private readonly ICacheManager _cache;
        public MerchantCategoryService(DataClient dataClient, ICacheManager cache) : base(dataClient)
        {
            this._cache = cache;
        }

        #region 获取所有分类
        /// <summary>
        /// 获取所有分类
        /// </summary>
        /// <param name="is_valid">1启用，2禁用</param>
        /// <returns></returns>
        private IList<mp_merchant_category> GetAll(int? is_valid = null)
        {
            return Queryable.Where(x => x.is_delete == 1)
                .WhereIF(is_valid != null, x => x.is_valid == is_valid.Value)
                .OrderBy(x => x.sortid)
                .ToList();
        }
        #endregion

        #region 获取商户类型Tree列表
        /// <summary>
        /// 获取商户类型Tree列表
        /// </summary>
        /// <param name="is_valid">1启用，2禁用</param>
        /// <param name="id">商户分类id，如果有值查询当前分类及子分类</param>
        /// <returns></returns>
        public IList<MerchantCategoryTreeModel> GetMerchantCategoryTree(int? is_valid = null, int id = 0)
        {
            var list = GetAll();
            if (list == null || list.Count == 0)
            {
                return null;
            }
            return MerchantCategoryTreeItem(list, id);
        }

        private List<MerchantCategoryTreeModel> MerchantCategoryTreeItem(IList<mp_merchant_category> list, int parentid)
        {
            return list.Where(x => x.parentid == parentid).Select((x, i) =>
            {
                return new MerchantCategoryTreeModel()
                {
                    id = x.id,
                    merchantcategoryname = x.merchantcategoryname,
                    parentid = x.parentid,
                    sortid = x.sortid,
                    is_valid = x.is_valid,
                    is_hot=x.is_hot,
                    children = list.Any(y => y.parentid == x.id) ? MerchantCategoryTreeItem(list, x.id).ToList() : null
                };
            }).ToList();
        }
        #endregion

        #region 根据id获取当前分类及其子分类
        /// <summary>
        /// 根据id获取当前分类及其子分类
        /// </summary>
        /// <param name="id">分类id</param>
        /// <returns></returns>
        public IList<mp_merchant_category> GetCurrentMerchantCategoryAndChildren(int id)
        {
            var list = GetAll();
            if (list == null || list.Count == 0)
            {
                return null;
            }
            IList<mp_merchant_category> data = new List<mp_merchant_category>();
            var model = list.FirstOrDefault(x => x.id == id);
            if (model?.id > 0)
            {
                data.Add(model);
                list.Where(x => x.parentid == model.id)?.ToList()?.ForEach(item=>
                {
                    data.Add(item);
                    list.Where(x => x.parentid == item.id)?.ToList()?.ForEach(item2 =>
                    {
                        data.Add(item2);
                    });
                });
                return data;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 查询热门商户类型
        /// <summary>
        /// 查询热门商户类型
        /// </summary>
        /// <param name="merchantcategoryparentid">上级id(0查询一级分类)</param>
        /// <param name="top">返回几条数据</param>
        /// <returns></returns>
        public IList<HotMerchantCategoryDto> GetHotMerchantCategoryList(int merchantcategoryparentid, int top = 5)
        {
            var queryable = dataClient.Queryable<mp_merchant_category>().Where(x => x.is_hot == 1);
            if (merchantcategoryparentid == -1)
            {
                queryable.Where(x => x.parentid == 0);
            }
            else
            {
                queryable.Where(x => x.parentid == merchantcategoryparentid);
            }
            return queryable.Select<HotMerchantCategoryDto>("id merchantcategoryid,merchantcategoryname,(select count(1) from mp_merchant where merchantcategory=mp_merchant_category.id) merchantcount")
                .OrderBy("merchantcount desc")
                .Take(top).ToList();
        }
        #endregion

    }
}
